package youleche.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

import youleche.com.connection.JdbcHelper;
import youleche.com.idao.IBaseParamInfo;
import youleche.com.model.MBaseParamInfo;

/**
 * 基础参数数据访问
 * @author Administrator
 *
 */
public class DBaseParamInfo implements IBaseParamInfo{

	/**
	 * 获取基础参数列表
	 * @param con 数据库连接
	 * @return 返回基础参数列表
	 * @throws Exception 抛出异常
	 */
	@Override
	public List<MBaseParamInfo> getBaseParamInfoList(Connection con)
			throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			String sql = "select KeyID,ParamName,ParamValue,AssociatedKeyID,Remark,AddTime,ModifyTime,IsDelete from BaseParamInfo where IsDelete=0;";
			ps = con.prepareStatement(sql);
			rs = null;
			rs = ps.executeQuery();
			List<MBaseParamInfo> list = new LinkedList<MBaseParamInfo>();
			MBaseParamInfo baseInfo = null;
			while(rs.next()) {
				baseInfo = new MBaseParamInfo();
				baseInfo.setKeyID(rs.getString(1));
				baseInfo.setParamName(rs.getString(2));
				baseInfo.setParamValue(rs.getString(3));
				baseInfo.setAssociatedKeyID(rs.getString(4));
				baseInfo.setRemark(rs.getString(5));
				baseInfo.setAddTime(rs.getDate(6));
				list.add(baseInfo);
			}
			
			return list;
		}finally{
			JdbcHelper.free(rs, ps);
		}
	}

	/**
	 * 添加基础信息
	 * @param con 数据库连接
	 * @param model 添加对象
	 * @return 返回添加影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int addBaseParamInfo(Connection con, MBaseParamInfo model)
			throws Exception {
		PreparedStatement pre = null;
		try{
			String sql = "INSERT baseparaminfo(KeyID,ParamName,ParamValue,AssociatedKeyID,Remark,IsDelete,AddTime,ModifyTime) VALUES(?,?,?,?,?,?,?,?);";
			pre = con.prepareStatement(sql);
			pre.setObject(1, model.getKeyID());
			pre.setObject(2, model.getParamName());
			pre.setObject(3, model.getParamValue());
			pre.setObject(4, model.getAssociatedKeyID());
			pre.setObject(5, model.getRemark());
			pre.setObject(6, model.getIsDelete());
			pre.setObject(7, model.getAddTime());
			pre.setObject(8, model.getModifyTime());
	
			return pre.executeUpdate();
		}finally{
			JdbcHelper.free(null, pre);
		}
	}
	
	/**
	 * 得到keyId通过值和name
	 * @param con 数据库连接
	 * @param paramValue 参数值
	 * @param paramName 参数名
	 * @return keyId
	 * @throws Exception 异常
	 */
	@Override
	public String getKeyIdByParamValueAndName(Connection con,
			String paramValue, String paramName) throws Exception {
		PreparedStatement pre = null;
		ResultSet rs = null;
		try{
			String sql = "select keyId from baseparaminfo where ParamValue='"+paramValue+"' and ParamName='"+paramName+"'  order by addTime desc limit 1;";
			pre = con.prepareStatement(sql);
			rs = pre.executeQuery();
			if(rs != null){
				while(rs.next()) {
					return rs.getString(1);
				}			
			}
			
			return "";
		}finally{
			JdbcHelper.free(rs, pre);
		}
	}

	/**
	 * 更新基础参数
	 * @param con 数据库连接
	 * @param info 基础参数
	 * @return 返回更新影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int updateBaseInfobyKeyId(Connection con, MBaseParamInfo info)
			throws Exception {
		PreparedStatement ps = null;
		try{
			String sql = "UPDATE baseparaminfo SET ParamName='"+info.getParamName()+"',ParamValue='"+info.getParamValue()+"',AssociatedKeyID='"+info.getAssociatedKeyID()+"',Remark='"+info.getRemark()+"',AddTime='"+info.getAddTime()+"' where KeyID='"+info.getKeyID()+"' and IsDelete='0' limit 1";
			ps = con.prepareStatement(sql);
			
			return ps.executeUpdate();
		}finally{
			JdbcHelper.free(null, ps);
		}
	}

}
